package wzh.crm.db;

import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.text.ParseException;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import wzh.crm.vo.Vo;

/**
 * Created by wangzehao on 2016/6/17.
 */
public abstract class WebServiceDB<T extends Vo> implements CURDDB<T> {
    public static Connection getConnection(String method){
        Connection c= Jsoup.connect("http://nqiwx.mooctest.net:8090/wexin.php/Api/Index/"+method);
        c.ignoreContentType(true);
        return c;
    }
    @Override
    public List<T> getAll() throws Exception {
        Connection c=getConnection(listQueryMethod());
        Document d=c.data("currentpage","1").data("search","").post();
        JSONObject pageInfo=new JSONObject(d.text());
        int pageCount=pageInfo.getInt("pagecount");
        List<T> result=new ArrayList<>();
        for(int page=1;page<=pageCount;page++){
            Connection thisPageConnection=getConnection(listQueryMethod());
            Document thisPageDocument=thisPageConnection.data("currentpage",Integer.toString(page)).data("search","").post();
            JSONObject thisPageJson=new JSONObject(thisPageDocument.text());

            int currentCount=thisPageJson.getInt("currentcount");
            for(int i=0;i<currentCount;i++) {
                result.add(toBean(thisPageJson.getJSONObject(Integer.toString(i))));
            }
        }
        return result;
    }

    @Override
    public T get(int id) throws Exception {
        Connection c=getConnection(idQueryMethod());
        Document d=c.data(idColumnName()+"id",Integer.toString(id)).get();
        JSONObject json=new JSONObject(d.text());
        int recordCount=json.getInt("recordcount");
        if(recordCount==1){
            return toBean(json.getJSONObject("0"));
        }
        return null;
    }

    @Override
    public List<T> getMine(int myId) throws Exception {
        List<T> all=getAll();
        return all;
    }

    @Override
    public void delete(T model) throws Exception {
    }

    @Override
    public void save(T model) throws Exception {
        Connection c;
        if(!model.hasId()){
            c=getConnection(createMethod());
        }else{
            c=getConnection(modifyMethod());
        }
        toPost(model,c);
        Document d=c.post();
        if(new JSONObject(d.text()).getInt("resultcode")!=0){
            System.err.println(d.text());
        }
    }

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    protected Date parseDate(String s) {
        try {
            return sdf.parse(s);
        }catch (ParseException e){
            e.printStackTrace();
            return null;
        }
    }

    protected String dateToString(Date date){
        return sdf.format(date);
    }

    abstract protected T toBean(JSONObject json) throws Exception;

    abstract protected void toPost(T model,Connection c);

    abstract protected String listQueryMethod();
    abstract protected String idQueryMethod();
    private String idColumnName(){
        return idQueryMethod().substring(0,idQueryMethod().indexOf("_"));
    }
    abstract protected String createMethod();
    abstract protected String modifyMethod();
}
